/****************************************************************************
 *
 *  ALi (Shanghai) Corporation, All Rights Reserved. 2007 Copyright (C)
 *
 *  File: mp_subtitle_srt.c
 *
 *  Description: This file describes srt file operations.
 *
 *  History:
 *      Date        	Author      	Version  		Comment
 *      ====        	======      =======  	=======
 *  1.  2009.5.29  Martin_Xia  	0.0.1  		
 *
 ****************************************************************************/

#include <types.h>
#include <retcode.h>
#include <api/libc/printf.h>

#include <api/libc/string.h>
#include <osal/osal.h>
#include <api/libfs2/stat.h>
#include <api/libfs2/dirent.h>
#include <api/libfs2/statvfs.h>
#include <api/libfs2/unistd.h>
#include <api/libfs2/stdio.h>

#include <api/libmp/media_player_api.h>
#include <api/libmp/mp_subtitle_api.h>
#include "mp_subtitle_vobsub.h"
#include "mp_subtitle.h"



#define MP_ENABLE_IGNORE_SRT_TAG
/**********************Global  Variables***********************************/
//define the Global  Variables


/**************************Static  Function Declare***********************************/
static void mp_subtitle_parse_utf8_srt(MpSubtitle *p,char *str, unsigned long *pos, unsigned long *time);
static void mp_subtitle_parse_unicode_srt(MpSubtitle *p, wchar_t *str, unsigned long *pos, unsigned long *time);


#if 0
#endif

/****************************API  Function ***********************************/
/////////////////////////////////////////////////////////////////////////////
// mp_subtitle_parse_srt
//
// Description:
//	API function. Parse unicode srt subtitle file.
//
// Arguments:
//	sub - Subtitle handle
//	str - Pointer to a srt file in memory
//	pos - Store every offset for each subtitle in a srt file
//	time - Store every time for each subtitle in a srt file
//	
// Return Value:
//	None
//
/////////////////////////////////////////////////////////////////////////////

void mp_subtitle_parse_srt(void *sub, char *str, unsigned long *pos, unsigned long *time)
{	
	MpSubtitle				*p;
	
	MP_SUB_DEBUG("====>>mp_subtitle_parse_srt()\n");

	if ((!sub)||(!str)||(!pos)||(!time))
	{
		MP_SUB_DEBUG("mp_subtitle_parse_srt: Invalid Parameters!\n");
		MP_SUB_DEBUG("<<====mp_subtitle_parse_srt()\n");
		return;
	}


	p = (MpSubtitle*)sub;

	if (p->avail_sub_files[p->current_sub_file_id].avail_file_type != MP_SUBTITLE_TYPE_SRT)
	{
		MP_SUB_DEBUG("mp_subtitle_parse_srt: The subtitle file is not a srt file!\n");
		MP_SUB_DEBUG("<<====mp_subtitle_parse_srt()\n");
		return;
	}

	

	if (p->txt_file.txt_type == MP_SUBTITLE_TXT_TYPE_L_UNICODE)
	{
		mp_subtitle_parse_unicode_srt(p, (wchar_t*)str, pos, time);
	}
	else if ((p->txt_file.txt_type == MP_SUBTITLE_TXT_TYPE_UTF8) || (p->txt_file.txt_type == MP_SUBTITLE_TXT_TYPE_ANSI))
	{
		mp_subtitle_parse_utf8_srt(p, str, pos, time);
	}

	
	MP_SUB_DEBUG("<<====mp_subtitle_parse_srt()\n");
	return;
}



#if 0
#endif

/**************************Internal Static  Function ***********************************/
/////////////////////////////////////////////////////////////////////////////
// mp_subtitle_parse_utf8_srt
//
// Description:
//	Parse utf8 srt subtitle file.
//
// Arguments:
//	sub - Subtitle handle
//	str - Pointer to a srt file in memory
//	pos - Store every offset for each subtitle in a srt file
//	time - Store every time for each subtitle in a srt file
//	
// Return Value:
//	None
//
/////////////////////////////////////////////////////////////////////////////

static void mp_subtitle_parse_utf8_srt(MpSubtitle *p,char *str, unsigned long *pos, unsigned long *time)
{
	char *bol=str;
	char *bot=0,*eot=0,*cot=0;
	long l=0;
	long semicolons;
	char *xtime1,*xtext;
	long tt;
	long long realtime;

	MP_SUB_DEBUG("====>>mp_subtitle_parse_unicode_srt()\n");

	while (*bol)
	{
		if (
			(*(bol+0)=='\r' || *(bol+0)=='\n') &&
			(*(bol+1)=='\r' || *(bol+1)=='\n') &&
			(
				(
				(*(bol+2)=='\r' || *(bol+2)=='\n') &&
				(*(bol+3)=='\r' || *(bol+3)=='\n')

				)
				||
				*(bol+0)==*(bol+1)
			)
		)
		{
			*bol='\0';
			bol++;
			continue;
		}
		if (l>MP_SUBTITLE_MAX_SUB_COUNT-3) break;
#ifdef MP_ENABLE_IGNORE_SRT_TAG		
		if (*bol=='<') {bot=bol; eot=0;}
		if (*bol=='>') eot=bol;
#endif
		if (bot && eot && eot-bot<50)
		{
			while (bot!=eot+1) {*bot=' '; bot++;}
			bot=eot=0;
		}
		if (bol==str || *(bol-1)=='\n' || *(bol-1)=='\r')
		{
			cot=bol;
			semicolons=0;
			while (*cot!='\n' && *cot!='\r' && *cot!='\0')
			{
				if (*cot==':') semicolons++;
				cot++;
			}
			if (*cot=='\0') {break;}
			if (semicolons!=4) {bol++;continue;}
			cot=bol;
			while ((*cot<'0' || *cot>'9') && *cot!='\0') cot++;
			if (*cot=='\0') {bol++; continue;}
			xtime1=cot;
			*time=0;
			tt=0; while (*xtime1>='0' && *xtime1<='9') {tt*=10; tt+=(*xtime1-0x30); xtime1++;}; xtime1++;
			*time+=tt; *time*=60;
			tt=0; while (*xtime1>='0' && *xtime1<='9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt; *time*=60;
			tt=0; while (*xtime1>='0' && *xtime1<='9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt; *time*=1000;
			tt=0; while (*xtime1>='0' && *xtime1<='9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt;
			realtime=*time;
			realtime*=TICKSPERSEC;
			//realtime/=1000;
			*time=(unsigned long)realtime;
			time++;
			cot=xtime1;
			while ((*cot<'0' || *cot>'9') && *cot!='\0') cot++;
			if (*cot=='\0') {bol++; continue;}
			xtime1=cot;
			*time=0;
			tt=0; while (*xtime1>='0' && *xtime1<='9') {tt*=10; tt+=(*xtime1-0x30); xtime1++;}; xtime1++;
			*time+=tt; *time*=60;
			tt=0; while (*xtime1>='0' && *xtime1<='9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt; *time*=60;
			tt=0; while (*xtime1>='0' && *xtime1<='9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt; *time*=1000;
			tt=0; while (*xtime1>='0' && *xtime1<='9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt;
			realtime=*time;
			realtime*=TICKSPERSEC;
			//realtime/=1000;
			*time=(unsigned long)realtime;
			time++;
			cot=xtime1;
			while (*cot!='\r' && *cot!='\n' && *cot!='\0') cot++;
			while (*cot=='\r' || *cot=='\n') cot++;
			if (*cot=='\0') {bol++; continue;}
			xtext=cot;
			*pos=cot-str;
			pos++;
			*pos=0;
			pos++;
			l+=2;
		}
		bol++;
	}
	*str='\0';
	*pos=0xFFFFFFFF;
	*time=0;
	l++;
	p->total_sub_count=l;
	MP_SUB_DEBUG("<<====mp_subtitle_parse_unicode_srt()\n");
}

/////////////////////////////////////////////////////////////////////////////
// mp_subtitle_parse_unicode_srt
//
// Description:
//	Parse unicode srt subtitle file.
//
// Arguments:
//	sub - Subtitle handle
//	str - Pointer to a srt file in memory
//	pos - Store every offset for each subtitle in a srt file
//	time - Store every time for each subtitle in a srt file
//	
// Return Value:
//	None
//
/////////////////////////////////////////////////////////////////////////////
static void mp_subtitle_parse_unicode_srt(MpSubtitle *p, wchar_t *str, unsigned long *pos, unsigned long *time)
{
	wchar_t *bol=str;
	wchar_t *bot=0,*eot=0,*cot=0;
	long l=0;
	long semicolons;
	wchar_t *xtime1,*xtext;
	long tt;
	long long realtime;

	MP_SUB_DEBUG("====>>mp_subtitle_parse_unicode_srt()\n");

	while (*bol)
	{
		if (
			(*(bol+0)==L'\r' || *(bol+0)==L'\n') &&
			(*(bol+1)==L'\r' || *(bol+1)==L'\n') &&
			(
				(
				(*(bol+2)==L'\r' || *(bol+2)==L'\n') &&
				(*(bol+3)==L'\r' || *(bol+3)==L'\n')

				)
				||
				*(bol+0)==*(bol+1)
			)
		)
		*bol=L'\0';
		if (l>MP_SUBTITLE_MAX_SUB_COUNT-3) break;
#ifdef MP_ENABLE_IGNORE_SRT_TAG				
		if (*bol==L'<') {bot=bol; eot=0;}
		if (*bol==L'>') eot=bol;
#endif		
		if (bot && eot && eot-bot<50)
		{
			while (bot!=eot+1) {*bot=L' '; bot++;}
			bot=eot=0;
		}
		if (bol==str || *(bol-1)==L'\n' || *(bol-1)==L'\r')
		{
			cot=bol;
			semicolons=0;
			while (*cot!=L'\n' && *cot!=L'\r' && *cot!=L'\0')
			{
				if (*cot==L':') semicolons++;
				cot++;
			}
			if (*cot==L'\0') {break;}
			if (semicolons!=4) {bol++;continue;}
			cot=bol;
			while ((*cot<L'0' || *cot>L'9') && *cot!=L'\0') cot++;
			if (*cot==L'\0') {bol++; continue;}
			xtime1=cot;
			*time=0;
			tt=0; while (*xtime1>=L'0' && *xtime1<=L'9') {tt*=10; tt+=(*xtime1-0x30); xtime1++;}; xtime1++;
			*time+=tt; *time*=60;
			tt=0; while (*xtime1>=L'0' && *xtime1<=L'9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt; *time*=60;
			tt=0; while (*xtime1>=L'0' && *xtime1<=L'9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt; *time*=1000;
			tt=0; while (*xtime1>=L'0' && *xtime1<=L'9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt;
			realtime=*time;
			realtime*=TICKSPERSEC;
			//realtime/=1000;
			*time=(unsigned long)realtime;
			time++;
			cot=xtime1;
			while ((*cot<L'0' || *cot>L'9') && *cot!=L'\0') cot++;
			if (*cot==L'\0') {bol++; continue;}
			xtime1=cot;
			*time=0;
			tt=0; while (*xtime1>=L'0' && *xtime1<=L'9') {tt*=10; tt+=(*xtime1-0x30); xtime1++;}; xtime1++;
			*time+=tt; *time*=60;
			tt=0; while (*xtime1>=L'0' && *xtime1<=L'9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt; *time*=60;
			tt=0; while (*xtime1>=L'0' && *xtime1<=L'9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt; *time*=1000;
			tt=0; while (*xtime1>=L'0' && *xtime1<=L'9') {tt*=10; tt+=(*xtime1-0x30);xtime1++;}; xtime1++;
			*time+=tt;
			realtime=*time;
			realtime*=TICKSPERSEC;
			//realtime/=1000;
			*time=(unsigned long)realtime;
			time++;
			cot=xtime1;
			while (*cot!=L'\r' && *cot!=L'\n' && *cot!=L'\0') cot++;
			while (*cot==L'\r' || *cot==L'\n') cot++;
			if (*cot==L'\0') {bol++; continue;}
			xtext=cot;
			*pos=cot-str;
			pos++;
			*pos=0;
			pos++;
			l+=2;
		}
		bol++;
	}
	*str=L'\0';
	*pos=0xFFFFFFFF;
	*time=0;
	l++;
	p->total_sub_count=l;
	MP_SUB_DEBUG("<<====mp_subtitle_parse_unicode_srt()\n");
}


